package org.jeecg.modules.relation.relclassstudent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.relation.relclassstudent.entity.RelClassStudent;
import org.jeecg.modules.relation.relclassstudent.mapper.RelClassStudentMapper;
import org.jeecg.modules.relation.relclassstudent.service.IRelClassStudentService;
import org.jeecg.modules.relation.relclassstudent.vo.RelClassesAndStudentsVO;
import org.jeecg.modules.student.vo.StuStudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: rel_class_student
 * @author: baochangrong
 * @Date: 2022-02-10
 * @Version: V1.0
 */
@Service
public class RelClassStudentServiceImpl extends ServiceImpl<RelClassStudentMapper, RelClassStudent> implements IRelClassStudentService {
    @Autowired
    private RelClassStudentMapper relClassStudentMapper;

    @Override
    public IPage<RelClassesAndStudentsVO> queryVo(Page<RelClassesAndStudentsVO> relClassesAndStudentsVOPage, RelClassesAndStudentsVO relClassesAndStudentsVO, String tenantId) {
        System.out.println(relClassesAndStudentsVO);
        return relClassesAndStudentsVOPage.setRecords(relClassStudentMapper.queryVo(relClassesAndStudentsVOPage,relClassesAndStudentsVO, tenantId));
    }

    @Override
    public boolean updateRelBycol(RelClassStudent relClassStudent) {
        //更新学生，调换班级：待补充：需先验证是否已有关系数据，有则更新，没有则插入
//        UpdateWrapper<ContractPackage> updateWrapper = new UpdateWrapper<ContractPackage>();
//        //修改语句
//        updateWrapper.set("actual_amount", package_total_amount);
//        updateWrapper.setSql("difference_amount = planned_amount - "+package_total_amount);
//        //条件
//        updateWrapper.eq("id", contractMgt.getPackageId());
//        return  update(updateWrapper);

        return true;
    }

    public int insertUpgradeRelClassStudent(RelClassesAndStudentsVO relClassesAndStudentsVO){
        return relClassStudentMapper.insertUpgradeRelClassStudent(relClassesAndStudentsVO);
    }
    /**
     *根据学生vo信息变更学生所在的班级
     * @param stuStudentVo
     */
    @Override
    public RelClassStudent saveByStuStudentVo(StuStudentVo stuStudentVo){
        RelClassStudent relClassStudent = new RelClassStudent();
        relClassStudent.setClassId(stuStudentVo.getTargetNewClassId());
        relClassStudent.setStudentId(stuStudentVo.getId());
        relClassStudent.setSchoolYearId(stuStudentVo.getSchoolYearId());
        relClassStudent.setSemesterId(stuStudentVo.getSemesterId());
        relClassStudent.setTenantId(stuStudentVo.getTenantId());
        relClassStudent.setClassStudentStatus(1);
        save(relClassStudent);

        return relClassStudent;
    }
    /**
     *根据学生vo信息变更学生所在的班级
     * @param stuStudentVo
     */
    @Override
    public RelClassStudent saveOrupdateByStuStudentVo(StuStudentVo stuStudentVo){
        RelClassStudent relClassStudent = new RelClassStudent(stuStudentVo);
        LambdaQueryWrapper<RelClassStudent> relClassStudentQueryWrapper = new LambdaQueryWrapper<RelClassStudent>();
        relClassStudentQueryWrapper.eq(RelClassStudent::getStudentId,stuStudentVo.getId());
        relClassStudentQueryWrapper.eq(RelClassStudent::getTenantId,stuStudentVo.getTenantId());
        List<RelClassStudent> relClassStudentList = list(relClassStudentQueryWrapper) ;// new RelClassStudent();//stuStudentVo);

        if(oConvertUtils.listIsEmpty(relClassStudentList)) {
            relClassStudent.setClassId(stuStudentVo.getTargetNewClassId());
            relClassStudent.setStudentId(stuStudentVo.getId());
            relClassStudent.setSchoolYearId(stuStudentVo.getSchoolYearId());
            relClassStudent.setSemesterId(stuStudentVo.getSemesterId());
            relClassStudent.setTenantId(stuStudentVo.getTenantId());
            relClassStudent.setClassStudentStatus(1);
            save(relClassStudent);
        }
        else if(relClassStudentList.size() ==1){
            relClassStudent = relClassStudentList.get(0);
            relClassStudent.setClassId(stuStudentVo.getTargetNewClassId());
            relClassStudent.setStudentId(stuStudentVo.getId());
            relClassStudent.setSchoolYearId(stuStudentVo.getSchoolYearId());
            relClassStudent.setSemesterId(stuStudentVo.getSemesterId());
            relClassStudent.setTenantId(stuStudentVo.getTenantId());
            relClassStudent.setClassStudentStatus(1);
            updateById(relClassStudent);
        }
        else{
            //1个学生属于多个班级的时候，需要设置学生所在的班级，把源班级找出来，再找新的班级
            relClassStudentQueryWrapper.eq(RelClassStudent::getClassId,stuStudentVo.getClassId());
            relClassStudentList = list(relClassStudentQueryWrapper) ;// new RelClassStudent();//stuStudentVo);
            relClassStudent = relClassStudentList.get(0);
            relClassStudent.setClassId(stuStudentVo.getTargetNewClassId());
            relClassStudent.setStudentId(stuStudentVo.getId());
            relClassStudent.setSchoolYearId(stuStudentVo.getSchoolYearId());
            relClassStudent.setSemesterId(stuStudentVo.getSemesterId());
            relClassStudent.setTenantId(stuStudentVo.getTenantId());
            relClassStudent.setClassStudentStatus(1);
            updateById(relClassStudent);
        }
        return relClassStudent;
    }
}
